/**     
* @Title: AppTestCast.java   
* @Package me.crawler.test.entry   
* @Description: TODO   
* @author xaoyaoyao
* @date 2019年1月19日 下午3:58:36
*/
package me.crawler.test.entry;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import me.crawler.Application;
import me.crawler.entity.SpiderSeed;
import me.crawler.entity.SpiderTask;
import me.crawler.enumerate.RequestMethodEnum;
import me.crawler.init.ReptileData;
import me.crawler.jedis.RedisService;
import me.crawler.util.Constants;

/**
 * @ClassName: AppTestCast
 * @Description: TODO
 * @author xaoyaoyao
 * @date 2019年1月19日 下午3:58:36
 * 
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class}) // 指定启动类
public class AppTestCast {

	String domain = "gushiwen.org";

	@Autowired
	private RedisService redisService;

	@Test
	@SuppressWarnings("unchecked")
	public void testRunner() throws Exception {
		setTask();
		Thread.sleep(1000);
		setSeeds();

		ReptileData.getSpiderSeeds();

		Map<String, String> data = redisService.hmget(Constants.SPIDER_SEED_KEYS);
		System.out.println(data);
	}

	private void setTask() {
		SpiderTask task = new SpiderTask();
		task.setRequestMethod(RequestMethodEnum.GET.getMethod());
		task.setUrl("https://www.gushiwen.org/shiju/");
		task.setDomain(domain);
		task.setId(1L);
		redisService.lpush(Constants.SPIDER_TASK_KEYS, task.toString());
	}

	private void setSeeds() throws IOException {
		SpiderSeed seed = new SpiderSeed();
		String pathName = Test.class.getResource("/poetry.xml").getFile().toString();
		String template = FileUtils.readFileToString(new File(pathName), "utf-8");
		seed.setTemplate(template);
		seed.setDomain(domain);
		seed.setName("古诗文网");
		seed.setId(1L);
		List<SpiderSeed> seeds = Arrays.asList(seed);

		Map<String, String> hash = new ConcurrentHashMap<String, String>();
		for (SpiderSeed s : seeds) {
			hash.put(s.getId() + "", s.toString());
			System.out.println(s.toString());
		}
		redisService.hmset(Constants.SPIDER_SEED_KEYS, hash, 30 * 60);
	}

}
